package com.example.demo.controller.supper;

import cn.hutool.db.Page;


import com.example.demo.PageResult;
import com.example.demo.PageResult2;
import com.example.demo.api.dto.PageDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.core.Local;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jiangjinwu
 * @version 1.0
 * @since 2025/10/20 17:25
 */

@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
@RequestMapping("/supper/tenant")
public class SupperController {
  @GetMapping("/home")
  public String index(){
    return "index";
  }
  @PostMapping("/search")
  public HttpEntity<PageResult<Map<String, Object> > > index2(@RequestBody PageDTO pageDTO){





    LocalDateTime now = LocalDateTime.now();
    List<Map<String, Object> > list = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
      Map<String, Object> map = new HashMap<>();
      map.put("name", "name" + i);
      map.put("mobile", "185703779" + i);
      map.put("storeType",   i%2+1);
      map.put("expireAt",   now);
      list.add(map);
    }
    Integer fromIndex =(pageDTO.getPageNO()-1) * pageDTO.getPageSize();
    Integer toIndex = fromIndex + pageDTO.getPageSize();
    if(list.size()<toIndex){
      toIndex = list.size()-1;
    }
    PageResult pageResult = new PageResult(pageDTO.getPageNO(), pageDTO.getPageSize(),list.size());
    pageResult.setRecords( list.subList(fromIndex, toIndex));
    return ResponseEntity.ok(pageResult);
  }

  @PostMapping("/search2")
  public HttpEntity<PageResult2<Map<String, Object> > > search2(@RequestBody PageDTO pageDTO){
    LocalDateTime now = LocalDateTime.now();
    List<Map<String, Object> > list = new ArrayList<>();
    for (int i = 0; i < 15; i++) {
      Map<String, Object> map = new HashMap<>();
      map.put("name", "name" + i);
      map.put("mobile", "185703779" + i);
      map.put("storeType",   i%2+1);
      map.put("expireAt",   now);
      list.add(map);
    }
    Integer fromIndex =(pageDTO.getPageNO()-1) * pageDTO.getPageSize();
    Integer toIndex = fromIndex + pageDTO.getPageSize();
    if(list.size()<toIndex){
      toIndex = list.size()-1;
    }
    PageResult2 pageResult = new PageResult2(pageDTO.getPageNO(), pageDTO.getPageSize(),list.size());
    pageResult.setData( list.subList(fromIndex, toIndex));
    pageResult.setDraw(pageDTO.getDraw());
    return ResponseEntity.ok(pageResult);
  }
}
